package com.ereport.controller.ADPController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.ADPDAO;
import com.ereport.dao.AccountsDAO;
import com.ereport.entity.ADP;
import com.ereport.entity.Accounts;
import com.ereport.entity.EndCustomers;
import com.ereport.entity.Executives;

@Controller
@RequestMapping("/adp")
public class AccDevPlanController {
	@RequestMapping(value="/open.do", method=RequestMethod.GET)
	public String open(HttpServletRequest req,HttpSession session,ModelMap model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){

			return "/jsp/login.jsp";
		}
		AccountsDAO adao=new AccountsDAO();
		Set<String> daset=new HashSet<String>();
		List<Accounts> aclist=new ArrayList<Accounts>();
		StringBuilder sb=new StringBuilder();
		try {
			aclist=adao.getAccounts(ex.getId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(aclist!=null){
			for(int i=0;i<aclist.size();i++){
				String org=aclist.get(i).getAccName();
				daset.add(org);
			}
			
			Iterator itr=daset.iterator();
			while(itr.hasNext()){
				Object obj=itr.next();
				String or=(String)obj;
				if(or.length()!=0){
				sb.append(or+"/");	
				}
						
			}
			}
			
			model.addAttribute("orglist", sb.toString());
		return "/jsp/adp.jsp";
	}
	@RequestMapping(value="/save.do", method=RequestMethod.POST)
	public ModelAndView insert(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		
		String adpName=req.getParameter("adpName");
		String accName=req.getParameter("accName");
		String owner=req.getParameter("owner");
		String orgType=req.getParameter("orgType");
		String indusType=req.getParameter("industryType");
		String activity=req.getParameter("activity");
		String revPerid=req.getParameter("reviewPerid");
		String visitFreq=req.getParameter("visitFreq");
		String contFreq=req.getParameter("contFreq");
		String designSpent=req.getParameter("designSpent");
		String prodSpent=req.getParameter("prodSpent");
		String electrComp=req.getParameter("electrComp");
		String cables=req.getParameter("cables");
		String connectors=req.getParameter("connectors");
		String devTools=req.getParameter("devTools");
		String MROProd=req.getParameter("MROProd");
		String fabrication=req.getParameter("fabrication");
		String testCalbr=req.getParameter("testCalbr");
		String testEquip=req.getParameter("testEquip");
		String others=req.getParameter("others");
		String custEndProd=req.getParameter("custEndProd");
		String comptInfo=req.getParameter("comptInfo");
		
		ADPDAO adao=new ADPDAO();
		ADP adp=null;
		
		try {
			adp=adao.getAdp(ex.getId(),accName);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int adpid=0;
		if(adp!=null){
			adpid=adp.getId();
			try {
				adp=new ADP(adpid,ex,adpName,accName,owner,orgType,indusType,activity,revPerid,visitFreq,contFreq,
						designSpent,prodSpent,electrComp,cables,connectors,devTools,MROProd,fabrication,testCalbr,testEquip,
						others,custEndProd,comptInfo);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
		try {
			adp=new ADP(ex,adpName,accName,owner,orgType,indusType,activity,revPerid,visitFreq,contFreq,
					designSpent,prodSpent,electrComp,cables,connectors,devTools,MROProd,fabrication,testCalbr,testEquip,
					others,custEndProd,comptInfo);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		try {
			adao.insert(adp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("redirect:open.do");
		model.addObject("success", "Successfully Inserted");
		return model;
	}
	@RequestMapping(value="/getAdp.do", method=RequestMethod.GET)
	public @ResponseBody
	String getAdp(HttpServletRequest req,HttpSession session){
		Executives ex=(Executives)session.getAttribute("loginUser");
		int exeId=ex.getId();
		String accName=req.getParameter("accName");
		ADPDAO adao=new ADPDAO();
		ADP adp=null;
		try {
			adp=adao.getAdp(exeId, accName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(adp!=null){
		String adpname=adp.getAdpName();
		String owner=adp.getOwner();
		String orgType=adp.getOrgType();
		String industType=adp.getIndustryType();
		String activity=adp.getActivity();
		String revperiod=adp.getReviewPeriod();
		String visitfreq=adp.getVisitfreq();
		String contfrq=adp.getContfreq();
		String designSpent=adp.getDesignSpent();
		String prodctionSpent=adp.getProductionSpent();
		String eleComponents=adp.getElectronicComponents();
		String cables=adp.getCables();
		String connectors=adp.getConnectors();
		String devpTools=adp.getDevlpTools();
		String MROProd=adp.getMROProd();
		String fabrication=adp.getFabrication();
		String testCalibration=adp.getTestCalibration();
		String testEquipment=adp.getTestEquipment();
		String othersSpent=adp.getOthersSpend();
		String custEndDetails=adp.getCustEndProdDetails();
		String competrInfo=adp.getCompetitorInfo();
		String adpObj=adpname+"/"+owner+"/"+orgType+"/"+industType+"/"+activity+"/"+revperiod+"/"+visitfreq+"/"+contfrq
						+"/"+designSpent+"/"+prodctionSpent+"/"+eleComponents+"/"+cables+"/"+connectors+"/"+devpTools
						+"/"+MROProd+"/"+fabrication+"/"+testCalibration+"/"+testEquipment+"/"+othersSpent+"/"+custEndDetails
						+"/"+competrInfo;
		return adpObj;
		}
		else return "";
	}
}
